package org.springsocial.domain;

import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.sort;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonManagedReference;
import org.springsource.util.OrderExpression;
import org.springsource.util.PlayerOrderExpression;

@Entity
@Table(name = "Team")
public class Team implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -644244819290702602L;
	@Id
	@Column(name = "id", nullable = false,unique=true)
	private Long id;
	
	@Column(name = "name")
	private String name;
	
	
	@JsonBackReference
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name = "countryCode", insertable = false, updatable = false)
	private Country country;
	
	@JsonIgnore
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name = "levelId", insertable = false, updatable = false)
	private Level level;
	
	@JsonManagedReference
	@OneToMany(fetch = FetchType.EAGER,mappedBy="team", orphanRemoval=true,cascade = { CascadeType.ALL })
	private Set<Player> players;

	public Team() {
		super();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public Level getLevel() {
		return level;
	}

	public void setLevel(Level level) {
		this.level = level;
	}

	public Set<Player> getPlayers() {
		return players;
	}

	public void setPlayers(Set<Player> players) {
		this.players = players;
	}

	public String getValue() {
		return name.replace(" ", "-").toLowerCase();
	}
	public List<Player> getOrderedPlayers(PlayerOrderExpression playerOrder, OrderExpression order) {
		
		List<Player> sort = new ArrayList<Player>(this.getPlayers().size());
		switch (playerOrder) {
		case FIRSTNAME:
			sort = sort(players, on(Player.class).getFirstName());
			break;
		case LASTNAME:
			sort = sort(players, on(Player.class).getLastName());
			break;
		case POPULAR:
			sort = sort(players, on(Player.class).getOrderExpression());
		default:
			sort = sort(players, on(Player.class).getFirstName());
			break;
		}
		
		if(order.equals(OrderExpression.ASC)){
			 Collections.reverse(sort);
			 return sort;
		}
		else{
			 return sort;
		}
		
	}

}
